﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LoadingSceneController : MonoBehaviour
{
    private static LoadingSceneController _instance;
    public static LoadingSceneController instance => _instance;
    private AsyncOperation m_Load;
    public NormalSceneLoadingAnim anim;

    private void Awake()
    {
        _instance = this;
    }

    // Start is called before the first frame update
    void Start()
    {
        if (anim == null) anim = GetComponentInChildren<NormalSceneLoadingAnim>();
        
        anim.PlayShowAnimation(null, () =>
        {
            m_Load = SceneManager.LoadSceneAsync(s_toLoadScene);
            s_toLoadScene = "";
        });
    }
    

    // Update is called once per frame
    void Update()
    {
        if (m_Load != null)
        {
            if (m_Load.isDone)
            {
                m_Load = null;
                anim.PlayHideAnimation(() =>
                {
                    SceneManager.UnloadSceneAsync("LoadingScene");
                });
            }
        }
    }

    static string s_toLoadScene;
    public static void LoadScene(string scene)
    {
        if (LoadingSceneController.instance) 
            return;
        
        GameDebug.LogError($"load scene:{scene}");
        
        SceneManager.LoadScene("LoadingScene", LoadSceneMode.Additive);
        s_toLoadScene = scene;
    }
}
